package com.lions.client.view.gui;

import java.util.Date;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.lions.client.view.audio.AudioOutput;
import com.lions.client.view.popup.MyPopupPanel;

/**
 * Creates a popup panel to alert user the rover needs
 * needs to be calibrated,
 * @author Eric
 *
 */
public class TenDayPopup extends MyPopupPanel{
	
	/**
	 * Constructor for Ten Day Alert popup
	 */
	public TenDayPopup(){
		
		// set the panel to the popup CSS styling
		vp.setStyleName("popup");
		 
		new AudioOutput().playInputError();
		createHandlers();
		
		vp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		
		vp.add(new Image("images/Alert.jpg"));
		vp.add(alertLabel1);
		vp.add(alertLabel2);
		vp.add(recal);
		
		// Add the panel with button and icon to the popup
		setWidget(vp);
	}

	/**
	 * Initializes handlers for recalibration button
	 */
	private void createHandlers(){
		recal.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event){
				recalibrate(getEndDate());
				//hide();
				recalSuccess();
			}
		});
		close.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event){
				hide();
				vp.clear();
			}
		});
	}
	
	/**
	 * Checks if the rover is due to be recalibrated
	 * @return true if panel should pop, false otherwise
	 */
	public boolean checkRecal(){
		if(endDate.equals(currentDate) || endDate.before(currentDate)){
			return true;
		}
		else {
			return false;
		}
	}
	
	
	/**
	 * Mutator method to recalibrate the rover.
	 * @param date the new startDate.
	 */
	private void recalibrate(Date date){
		startDate = currentDate;
		endDate = nextDate; 
	}
	
	/**
	 * Checks whether the recalibration was successful.
	 * If successful, outputs popup with new calibration date.
	 */
	public void recalSuccess(){
		if (currentDate == startDate) {
			vp.clear();	
			vp.add(new Image("images/checkIcon.jpg"));
			vp.add(recalLabel1);
			vp.add(recalLabel2);
			vp.add(close);		
		}
	}
	
	/**
	 * Accessor method for end date of alert
	 * @return endDate
	 */
	public Date getEndDate(){
		return endDate;
	}
	
/**
 * Panel for the popup to add to.
 */
private VerticalPanel vp = new VerticalPanel();	
/**
 * Starting date for rover calibration counter.
 */
private Date startDate = DateTimeFormat.getFormat("MM-dd-yyyy").parse("12-02-2013");
/**
 * Ending date for rover calibration counter.
 */
private Date endDate = DateTimeFormat.getFormat("MM-dd-yyyy").parse("12-12-2013");
/**
 * Current date used in rover recalibration.
 */
private Date currentDate = DateTimeFormat.getFormat("MM-dd-yyyy").parse("12-12-2013");
/**
 * Next date to recalibrate rover to.
 */
private Date nextDate = DateTimeFormat.getFormat("MM-dd-yyyy").parse("12-22-2013");
/**
 * String format of startDate.
 */
@SuppressWarnings("deprecation")
private String startDateString = DateTimeFormat.getShortDateFormat().format(startDate);
/**
 * String format of endDate.
 */
@SuppressWarnings("deprecation")
private String endDateString = DateTimeFormat.getShortDateFormat().format(endDate);
/**
 * String format of currentDate.
 */
@SuppressWarnings("deprecation")
private String curDateString = DateTimeFormat.getShortDateFormat().format(currentDate);
/**
 * String format of nextDate.
 */
@SuppressWarnings("deprecation")
private String nextDateString = DateTimeFormat.getShortDateFormat().format(nextDate);
/**
 * Recalibrate button constructor.
 */
private Button recal = new Button("Recalibrate");
/**
 * Close button constructor.
 */
private Button close = new Button("Close");
/**
 * Label used for the alert popup.
 */
private Label alertLabel1 = new Label("The Mars Rover must be recalibrated!");
/**
 * New label used as line break in alert popup.
 */
private Label alertLabel2 = new Label("Last calibration was: " + startDateString);
/**
 * Label used for the confirmation popup.
 */
private Label recalLabel1 = new Label("Recalibration successful!");
/**
 * New label used as line break in confirmation popup.
 */
private Label recalLabel2 = new Label("Next calibration: " + nextDateString);

}
